#include "MessageProcessor.h"


#include <assert.h>

MessageProcessor::MessageProcessor()
{
    m_SystemMessages = 0;
    m_World = 0;
    m_Renderer = 0;
    m_System = 0;
    m_MouseMessage.valid = 0;
}

MessageProcessor::~MessageProcessor()
{

}

void MessageProcessor::SetSystemMessages(SystemMessages *sysMess)
{
    m_SystemMessages = sysMess;
}

MessageProcessor &MessageProcessor::GetInstance()
{
	static MessageProcessor m_Instance;
	return m_Instance;
}

void MessageProcessor::SetWorld(World *world)
{
    m_World = world;
}

void MessageProcessor::SetRenderer(Renderer *renderer)
{
    m_Renderer = renderer;
}
/*
void MessageProcessor::AddSystemMessage(SystemMessage sysMess)
{
 //   m_SystemMessages.push_back(sysMess);
}
*/
void MessageProcessor::ClearSystemMessages()
{
  //  m_SystemMessages.clear();
}

WorldMessages& MessageProcessor::GetWorldMessages()
{
    m_WorldMessages.clear();

 //   for (SystemMessages::iterator sysMess = m_SystemMessages.begin(); sysMess != m_SystemMessages.end(); sysMess++) {
  //      getWorldMessages(*sysMess);
  //  }

    return m_WorldMessages;
}
/*
WorldMessages& MessageProcessor::getWorldMessages(SystemMessage sysMess)
{
    WorldMessage worldMsg;

    switch (sysMess.GetType())
    {
        case smMOUSE_MOVED:
            worldMsg.Set( wmLIGHT_SURFACE, sysMess.GetPoint());
            m_WorldMessages.push_back(worldMsg);
            break;
        case smLEFT_MB_PRESS:
            worldMsg.Set( wmDESELECT_ALL, sysMess.GetPoint());
            m_WorldMessages.push_back(worldMsg);
            worldMsg.Set( wmPOINT_ON_MAP, sysMess.GetPoint());
            m_WorldMessages.push_back(worldMsg);
            break;
        case smRIGHT_MB_PRESS:
            break;
        case smKEY_A_PRESSED:
            break;
        case smKEY_A_RELEASED:
            break;
        case smKEY_B_PRESSED:
            break;
        case smKEY_B_RELEASED:
            break;
        default:
            break;
    }

    return m_WorldMessages;
}
*/
CameraMessages& MessageProcessor::GetCameraMessages()
{
    m_CameraMessages.clear();

    for (SystemMessages::iterator sysMess = m_SystemMessages->begin(); sysMess != m_SystemMessages->end(); sysMess++) {
        getCameraMessages(*sysMess);
    }

    return m_CameraMessages;
}

CameraMessages& MessageProcessor::getCameraMessages(SystemMessage sysMess)
{
    CameraMessage camMsg;

    switch (sysMess.GetType())
    {
        case smLEFT_MB_PRESS:
            camMsg.Set( formMessage(sysMess.GetX(), sysMess.GetY()), vector2i(sysMess.GetX(), sysMess.GetY()));
            m_CameraMessages.push_back(camMsg);
            break;
        case smLEFT_MB_RELEASE:
            camMsg.Set( cmSTRAFE_STOP, vector2i(sysMess.GetX(), sysMess.GetY()));
            m_CameraMessages.push_back(camMsg);
            camMsg.Set( cmMOVE_STOP, vector2i(sysMess.GetX(), sysMess.GetY()));
            m_CameraMessages.push_back(camMsg);
            break;
        case smRIGHT_MB_PRESS:
            break;
        case smKEY_Q_PRESSED:
            camMsg.Set( cmROLL_FORWARD, vector2i(sysMess.GetX(), sysMess.GetY()));
            m_CameraMessages.push_back(camMsg);
            break;
        case smKEY_Q_RELEASED:
            camMsg.Set( cmROLL_STOP, vector2i(sysMess.GetX(), sysMess.GetY()));
            m_CameraMessages.push_back(camMsg);
            break;
        case smKEY_A_PRESSED:
            camMsg.Set( cmROLL_BACKWARD, vector2i(sysMess.GetX(), sysMess.GetY()));
            m_CameraMessages.push_back(camMsg);
            break;
        case smKEY_A_RELEASED:
            camMsg.Set( cmROLL_STOP, vector2i(sysMess.GetX(), sysMess.GetY()));
            m_CameraMessages.push_back(camMsg);
            break;
        default:
            break;
    }

    return m_CameraMessages;
}

ECameraMessage MessageProcessor::formMessage(int x, int y)
{/*
    assert(m_Renderer);
    int screenWidth = m_Renderer->GetScreenWidth();
    int screenHeight = m_Renderer->GetScreenHeight();

    if (x < screenWidth * 0.1)
        return cmSTRAFE_LEFT;
    if (x > screenWidth * 0.9)
        return cmSTRAFE_RIGHT;
    if (y < screenHeight * 0.1)
        return cmMOVE_FRONT;
    if (y > screenHeight * 0.9)
        return cmMOVE_BACK;
    */
    return cmEMPTY;
}

MouseMessage MessageProcessor::GetMouseMessage()
{
    m_MouseMessage.valid = 0;

    for (SystemMessages::iterator sysMess = m_SystemMessages->begin(); sysMess != m_SystemMessages->end(); sysMess++) {
        if ((*sysMess).GetType() == smLEFT_MB_PRESS)
        {
            m_MouseMessage.valid        = 1;
            m_MouseMessage.lmbClick     = 1;
            m_MouseMessage.lmbRelease   = 0;
            m_MouseMessage.rmbClick     = 0;
            m_MouseMessage.rmbRelease   = 0;
            m_MouseMessage.mpos_x = (*sysMess).GetX();
            m_MouseMessage.mpos_y = (*sysMess).GetY();
        }
        else if ((*sysMess).GetType() == smLEFT_MB_RELEASE)
        {
            m_MouseMessage.valid        = 1;
            m_MouseMessage.lmbClick     = 0;
            m_MouseMessage.lmbRelease   = 1;
            m_MouseMessage.rmbClick     = 0;
            m_MouseMessage.rmbRelease   = 0;
            m_MouseMessage.mpos_x = (*sysMess).GetX();
            m_MouseMessage.mpos_y = (*sysMess).GetY();
        }
    }
    return m_MouseMessage;
}

bool MessageProcessor::IsLeftMouseButtonPressed()
{
    return haveMessage(smLEFT_MB_PRESS);
}

bool MessageProcessor::haveMessage(ESystemMessage mess)
{
    for (SystemMessages::iterator sysMess = m_SystemMessages->begin(); sysMess != m_SystemMessages->end(); sysMess++) {
        if ((*sysMess).GetType() == mess)
            return 1;
    }
    return 0;
}

vector2i MessageProcessor::GetMosePosition()
{
    vector2i mousePos;

    mousePos.X = m_System->GetMousePosition()->X;
    mousePos.Y = m_System->GetMousePosition()->Y;

    return mousePos;
}

void MessageProcessor::SetSystem(System *sys)
{
    m_System = sys;
}
